package ar.com.solidussource.howmuchservices.web.controller;

import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import ar.com.solidussource.framework.backend.bo.exception.BusinessException;
import ar.com.solidussource.framework.backend.dao.filter.Page;
import ar.com.solidussource.framework.backend.dao.filter.Result;
import ar.com.solidussource.framework.backend.dao.filter.TableOrder;
import ar.com.solidussource.howmuchservices.backend.bo.ProductCRUDBO;
import ar.com.solidussource.howmuchservices.backend.bo.ProductsubcategoryCRUDBO;
import ar.com.solidussource.howmuchservices.common.entities.Product;
import ar.com.solidussource.howmuchservices.common.entities.Productsubcategory;
import ar.com.solidussource.howmuchservices.common.entities.filter.ProductFilter;
import ar.com.solidussource.howmuchservices.utils.json.JSONUtils;

@Controller
@RequestMapping("/product")
public class ProductController extends BaseController {
	private static final Logger log = Logger.getLogger(ProductController.class);
	private static final String TABLEORDER_CRITERION = "name";
	private static final String TABLEORDER_DIRECTION = "desc";
	
	@Autowired ProductCRUDBO productCRUDBO; 
	@Autowired ProductsubcategoryCRUDBO productsubcategoryCRUDBO;
	
	@RequestMapping(value="/show.json", method = RequestMethod.GET, produces="application/json")
	@ResponseBody
	public String getProductById(HttpServletResponse httpServletResponse, @RequestParam Integer productId) {
	
		try {
			Product product = productCRUDBO.getById(productId);
			
			return JSONUtils.getJSON(product);
			
		} catch (Exception e) {
			httpServletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			log.error(e.getMessage(), e);			
			return getJsonStatus(e.getMessage());
		}		

	}	
	
	@RequestMapping(value="/list.json", method = RequestMethod.GET, produces="application/json")
	@ResponseBody
	public String getProducts(HttpServletResponse httpServletResponse, @RequestParam Integer productSubCategoryId, @RequestParam Integer pageNumber, @RequestParam Integer size) {
	
		try {
			Productsubcategory productsubcategory = productsubcategoryCRUDBO.getById(productSubCategoryId);
			
			if (productsubcategory != null) {
				ProductFilter productFilter = new ProductFilter();
				productFilter.setProductSubCategoryId(productSubCategoryId);
				productFilter.setTableOrder(new TableOrder(TABLEORDER_CRITERION, TABLEORDER_DIRECTION));
				
				Page page = new Page(pageNumber, size);
				
				Result<Product> products = productCRUDBO.getAll(productFilter, page);
				
				return JSONUtils.getJSON(products.getResult());
				
			} else {
				throw new BusinessException("no productsubcategory "+productSubCategoryId);
			}
			
		} catch (Exception e) {
			httpServletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			log.error(e.getMessage(), e);			
			return getJsonStatus(e.getMessage());
		}

	}
	
	@RequestMapping(value="/create.json", method = RequestMethod.PUT, produces="application/json")
	@ResponseBody
	public String createProduct(HttpServletResponse httpServletResponse, @RequestParam Integer productSubCategoryId, @RequestParam String name, @RequestParam(required = false) String description) {
		
		try {
			Productsubcategory productsubcategory = productsubcategoryCRUDBO.getById(productSubCategoryId);
			
			if (productsubcategory != null) {
				Product product = new Product();
				
				product.setName(name);
				product.setDescription(description);
				product.setProductsubcategory(productsubcategory);
				product.setDeleted(false);
				
				product.setCreateuser(getCurrentUser().getId());				
				product.setCreatedate(new Date());
				
				productCRUDBO.saveNew(product);
				
				return getJsonStatus(RESPONSE_OK);
				
			} else {
				throw new BusinessException("no productsubcategory "+productSubCategoryId);
			}
			
		} catch (Exception e) {
			httpServletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			log.error(e.getMessage(), e);			
			return getJsonStatus(e.getMessage());
		}

	}
	
	@RequestMapping(value="/update.json", method = RequestMethod.PUT, produces="application/json")
	@ResponseBody
	public String updateProduct(HttpServletResponse httpServletResponse, @RequestParam Integer productId, @RequestParam Integer productSubCategoryId, @RequestParam String name, @RequestParam(required = false) String description) {
		
		try {
			Product product = productCRUDBO.getById(productId);
			
			if (product != null) {
				
				Productsubcategory productsubcategory = productsubcategoryCRUDBO.getById(productSubCategoryId);
				
				if (productsubcategory != null) {
										
					product.setName(name);
					product.setDescription(description);
					product.setProductsubcategory(productsubcategory);
					
					product.setUpdateuser(getCurrentUser().getId());				
					product.setUpdatedate(new Date());
					
					productCRUDBO.saveOrUpdate(product);
					
					return getJsonStatus(RESPONSE_OK);
					
				} else {
					throw new BusinessException("no productsubcategory "+productSubCategoryId);
				}						
				
			} else {
				throw new BusinessException("no product "+productId);				
			}
						
		} catch (Exception e) {
			httpServletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			log.error(e.getMessage(), e);			
			return getJsonStatus(e.getMessage());
		}

	}	
	
}
